package com.wframe.project.jnd.services.impl;

import com.wframe.project.jnd.vo.Coupon;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.dao.CouponDao;
import com.wframe.project.jnd.rest.util.ClientException;
import com.wframe.project.jnd.services.CouponService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;


@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponDao couponDao;

    public PageResult queryCoupon(String userid, Integer state, int ordinal, int size, int pageno) {
        JdataBean parm = new JdataBean();
        parm.put("userid", userid);
        parm.put("state", state);
        parm.put("containBefore", Integer.valueOf(1));
        parm.put("source", ordinal);
        return couponDao.queryCoupon(parm, size, pageno);
    }

    /**
     * 计算优惠券各条件数量
     */
    public List<JdataBean> countCoupon(String userid) {
        return couponDao.countCoupon(userid);
    }

    public String getUsableCount(String userid, int source) {
        List<JdataBean> jdataBean = couponDao.getUsableCount(userid, source);
        String usableCount = "";
        for (JdataBean jdataBean1 : jdataBean) {
            usableCount = jdataBean1.getString("counUsableCoupon");
        }
        return usableCount;
    }

    public PageResult getUsableCoupon(JdataBean parms, int size, int pno) {
        return couponDao.getUsableCoupon(parms, size, pno);
    }

    /**
     * 根据订单金额查询可用优惠劵
     */
    public PageResult queryCanCoupon(String userid, Double ordermoney, int ordinal, int size, int pageno) {
        JdataBean parm = new JdataBean();
        parm.put("userid", userid);
        parm.put("state", 1);
        parm.put("containBefore", Integer.valueOf(0));
        parm.put("ordermoney", ordermoney);
        parm.put("source", ordinal);
        return couponDao.queryCoupon(parm, size, pageno);
    }

    /**
     * 查询有效但不可用（未到使用时间、金额不足、类别不适用）优惠券
     */
    public PageResult queryDisCoupon(String userid, Double ordermoney, int ordinal, int size, int pageno) {
        JdataBean parm = new JdataBean();
        parm.put("userid", userid);
        parm.put("state", 1);
        parm.put("ordermoney", ordermoney);
        parm.put("source", ordinal);
        return couponDao.queryDisCoupon(parm, size, pageno);
    }

    public void addCoupon(String userid, String code) throws Exception {
        if (StringUtils.isBlank(code)) throw new ClientException("请输入优惠劵号");
        JdataBean bean = couponDao.getCouponByCode(code);
        if (bean == null) throw new ClientException("优惠劵号不正确");
        else if (bean != null && bean.getString("userid").equals(userid)) throw new ClientException("优惠劵号不正确");
    }

    public JdataBean getCoupon(String id) throws Exception {
        return couponDao.getCoupon(id);
    }

    /**
     * 标记优惠劵已使用
     */
    public void tagCouponUsed(String id) throws Exception {
        couponDao.tagCouponUsed(id);
    }

    @Override
    public List<Map> findCanCouponList(String userId, Double orderMoney, int ordinal) {
        JdataBean parms = new JdataBean();
        parms.put("userid", userId);
        parms.put("ordermoney", orderMoney);
        parms.put("source", ordinal);
        return couponDao.findAll(parms);
    }

    @Override
    public boolean isUsable(String couponid, String userid, int ordinal, double paymoney) {
        return couponDao.isUsable(couponid, userid, ordinal, paymoney);
    }

    @Override
    public boolean isExist(String userid, double payMoney, int ordinal) {
        return couponDao.isExist(userid, payMoney, ordinal);
    }

    @Override
    public List<Coupon> findReceivedCoupons(String userId, int platform) {
        return couponDao.findReceivedCoupons(userId, platform);
    }

    @Override
    public List<Coupon> findAvailableCoupons(String userId, int platform) {
        return couponDao.findAvailableCoupons(userId, platform);
    }

    @Override
    public List<Coupon> findAll(String userid, String productid, int platform) {
        return couponDao.findAll(userid, productid, platform);
    }

    @Override
    @Transactional
    public void receiveCoupon(JdataBean coupon, String userId, int sourceType) {
        try {
            int couponId = coupon.getInt("id");
            coupon.remove("id");
            coupon.remove("num");
            coupon.put("tag", 1);
            coupon.put("state", 1);
            coupon.put("userid", userId);
            coupon.put("create_time", new Date());
            coupon.put("get_couponid", couponId);
            coupon.put("get_source", sourceType);
            couponDao.save("wb_coupon", coupon.getDataMap());
            couponDao.updateSales(couponId);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    @Override
    public JdataBean findOne(String userId, Integer couponId, int platform) {
        return couponDao.findOne(userId, couponId, platform);
    }

    @Override
    public List<Coupon> findOrderCoupons(Set<String> ids, String userid, int platform) {
        Map parms = new HashMap();
        parms.put("ids", ids);
        parms.put("userid", userid);
        parms.put("platform", platform);
        return couponDao.findOrderCoupons(parms);
    }

}